/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/11/25 19:58
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */


(function () {

    "use strict";

    var api = {
        submitTicket: "/promoter/ticket/addTicket"
    };


    sm.controller("admin-promoter-ticket-add-controller", ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.ticket = {
            name: "",
            wechat: "",
            app: "",
            num: "",
            amount: "",
            description: "",
            limitAmount: ""
        };

        var bak = angular.copy($scope.ticket);

        $scope.timepicker = {
            timestart: moment().subtract('hours').format('YYYY-MM-DD 00:00:00'),
            timeend: moment().subtract('month', -1).format('YYYY-MM-DD 23:59:59')
        };

        $('#reservationtime').daterangepicker({
            // autoApply: true,
            timePicker24Hour: true,
            opens : 'right',
            locale: {
                format: 'YYYY-MM-DD HH:mm:ss',
                applyLabel : '确定',
                cancelLabel : '取消',
                fromLabel : '起始时间',
                toLabel : '结束时间',
                customRangeLabel : '自定义',
                daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
                monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
                    '七月', '八月', '九月', '十月', '十一月', '十二月' ],
                firstDay : 1
            }
        }, function (start, end, label) {
            $scope.timepicker.timestart = start.format('YYYY-MM-DD HH:mm:ss');
            $scope.timepicker.timeend = end.format('YYYY-MM-DD HH:mm:ss');
            $scope.times = $scope.timepicker.timestart + ' - ' + $scope.timepicker.timeend;
            $scope.$apply();
        });

        $scope.times = $scope.timepicker.timestart + ' - ' + $scope.timepicker.timeend;

        /**
         * 提交
         */
        $scope.saveTicket = function () {

            if (!$scope.ticket.name) {
                Modal.alert("名称不能为空");
                return false;
            }

            if (!$scope.ticket.app && !$scope.ticket.wechat) {
                Modal.alert("请选择使用平台");
                return false;
            }

            if (!$scope.ticket.amount) {
                Modal.alert("优惠金额不能为0");
                return false;
            }

            if (!$scope.ticket.limitAmount) {
                Modal.alert("最小订单金额不能为0");
                return false;
            }
            if (!$scope.ticket.description) {
                Modal.alert("使用描述必须填写");
                return false;
            }

            var data = {};
            data = angular.extend($scope.ticket, $scope.timepicker);
            var subData = angular.copy(data);

            subData.app = data.app ? 1 : 0;
            subData.wechat = data.wechat ? 1 : 0;

            subData.num = parseInt(data.num);
            subData.amount = parseInt(data.amount * 100);
            subData.limitAmount = parseInt(data.limitAmount * 100);

            $http.post(api.submitTicket, subData).success(function (res) {
                Modal.success({
                    title: "添加成功",
                    buttons: [{
                        name: '返回列表',
                        type: 'cancel',
                        event: function () {
                            location.href = "/promoter/ticket/index"
                        }
                    }, {
                        name: "继续添加",
                        event: function () {
                            $scope.ticket = angular.copy(bak);
                        }
                    }]
                });
            });


        };


    }]);
})();
